<?php
namespace app\index\controller;
use think\Controller;
use app\index\controller\Common;

class Article extends Common
{
    public function index(){
		$q_nid 	 = getNID();
		$q_cid   = 0;	
		$title   = urldecode(input('title'));
		$cid     = "";
		if($q_nid!=""){		
			$nid = db('nav')->where("entitle",$q_nid)->value("id");			
			if($nid==""){
				$res1 = db('cate')->field('nid,id')->where("entitle",$q_nid)->find();								
				$nid  = $res1['nid'];
				$cid  = $res1['id'];  
				$q_cid = 1;	//是分类
			}
		}	
		if($nid==""){ exit("error");}				
		$navrow = db('nav')->where('id',$nid)->find();	
		$data["nid"] 	 = $navrow['id'];
		$data["banner"]	 = isMobile() ? $navrow['img1'] : $navrow['img'];
		$data["columnName"]	 = $navrow['title'];
		$data["showcate"]	 = $navrow['showcate'];
		$data["entitle"] = $navrow['entitle'];	
			
		//无分类直接显示文章
		if($navrow['showcate']==0){			
			$data["leftlist"] = db('article')->field('id,title')->where('nid',$nid)->order("sort,id")->select();			
			$id  = db('article')->where('nid',$nid)->order('sort,id')->value('id');
			if($id=="") exit('未添加任何文章');
			$data['one'] = db('article')->where('id',$id)->find();
			$data['id'] = $id;		
		}
		
		//有分类直接显示列表
		if($navrow['showcate']==1){
			$where['nid'] = $nid;
			$data["leftlist"] = webtreelist('cate','title,img,id,entitle,content',['nid'=>$nid]);											
			if($q_cid==1 && $cid != ""){					
				$where['cid'] = array("in",treecatewhere($cid));
				$data["cid"] = $cid;	
			}			
			if($title!=""){				
				$where['title'] = array('like','%'.$title.'%');
			}		
			
			$order = "istop desc,sort desc,id desc";			
			$list = db('article')->where($where)->order($order)->paginate(9,false,['query' => request()->param()]);												
			$data["list"] = $list;
			$data["page"] = $list->render();			
		}
		$data["site"] = getseo($nid,"",$cid);
		$data["cateName"] = getCateName($cid,$nid); 	
		$this->assign($data);
		
		//模板定义
		$ntpl  = $navrow['showcate']==1?$navrow['list_tpl']:$navrow['msg_tpl'];		
		$mtpl = db('module')->where('id',$navrow['mid'])->value('tpl');				
		$tpl  = $ntpl ? $ntpl : $mtpl;		
		return $this->fetch($navrow['showcate']==1?$tpl:'show:'.$tpl);			
    }	
	
}
